import { _decorator, Component, Node, Button, Label, v3, Vec3 } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { adsm } from '../../../../scripts/managers/AdsManager';
import { udm } from '../../../../scripts/managers/UserDataManager';
import { MSResolve, MSRToString_tm_s } from '../../../../scripts/libs/utils/date';
import { DevConfig } from '../../../../scripts/managers/DevConfig';
import { IntervalStartNow } from '../../../../scripts/libs/rxjs/cc3/CreatorSchedule';
import { ShopPanel } from '../ShopPanel/ShopPanel';
import { thinking_sdk_m } from '../../../../scripts/managers/SDK/ThinkingCocosSDKManager';
import { gm } from '../../../../scripts/managers/GameManager';
import { MergeASAP } from '../../../../scripts/libs/rxjs/cc3/Merge';
import { lm } from '../../LobbyManager';
import { forkJoin, Observable } from 'rxjs';
import { TweenToStart } from '../../../../scripts/libs/rxjs/cc3/TweenStart';
const { ccclass, property } = _decorator;

@ccclass('BuyHeartPanel')
export class BuyHeartPanel extends BasePanel {

    openWay: string = 'Result';

    @property(Node)
    mainNode: Node = null;

    @property(Button)
    closeButton: Button = null;

    @property(Button)
    adsButton: Button = null;

    @property(Button)
    refillButton: Button = null;

    @property(Label)
    refillButtonLabel: Label = null;

    @property(Node)
    remainLayoutNode: Node = null;

    @property(Label)
    remainLabel: Label = null;

    @property(Label)
    heartLabel: Label = null;

    willBind(): void {
        gm.currentAdsType.value = 'AdEnergy';
        gm.currentAdsOpenUI.value = 'BuyHeartPanel';
        thinking_sdk_m.requestTrack('Ads_Show', {
            AdsType: 'AdEnergy',
            OpenUI: 'BuyHeartPanel',
            IsNew: udm.isNew.value
        });
        // this.adaptive();
        this.mainNode.setScale(.5, .5, .5);
    }

    useObserves() {
        return [
            BindButton(this.closeButton).subscribe(this.closeButtonClicked.bind(this)),
            BindButton(this.adsButton).subscribe(this.adsButtonClicked.bind(this)),
            BindButton(this.refillButton).subscribe(this.refillButtonClicked.bind(this)),
            udm.heart.subscribe(this.refresh.bind(this)),
            IntervalStartNow(1, this.node).subscribe(this.refresh.bind(this)),

            MergeASAP(udm.heart, gm.heartAnimatingFlags, udm.infHeartTill).subscribe(this.refresh.bind(this)),
        ];
    }

    // adaptive() {
    //     this.mainNode.setScale(v3(Vec3.ONE).multiplyScalar(lm.getAdaptiveScale()));
    // }

    openPanelAnim(): Observable<any> {
        return forkJoin([
            TweenToStart(this.ensureUIOpacity(), .2, { opacity: 255 }, { easing: 'expoOut' }),
            TweenToStart(this.mainNode, .4, { scale: lm.getAdaptiveScaleV3() }, { easing: 'backOut' }),
        ]);
    }

    closePanelAnim(): Observable<any> {
        this.ensureUIOpacity();
        return forkJoin([
            TweenToStart(this.ensureUIOpacity(), .2, { opacity: 0 }, { easing: 'expoIn' }),
            TweenToStart(this.mainNode, .2, { scale: v3(.5, .5, .5) }, { easing: 'backIn' }),
        ]);
    }

    refresh() {
        let curHeartCount = udm.getHeartCount();
        this.heartLabel.string = `${curHeartCount}`;
        let remainText = null;
        let remainMs = udm.getRemainMsToRecoverOneHeart();
        if (remainMs != null) {
            let msr = MSResolve(remainMs);
            remainText = MSRToString_tm_s(msr);
        }
        if (remainMs) {
            this.remainLabel.string = `${remainText}`;
            this.remainLayoutNode.active = true;
        } else {
            this.remainLayoutNode.active = false;
        }

        this.refillButtonLabel.string = `${DevConfig.HEART_REFILL_NEED_COIN}`;
    }

    closeButtonClicked() {
        BuyHeartPanel.CloseProcess(null, 'close');
    }

    refillButtonClicked() {
        if (udm.coin.value < DevConfig.HEART_REFILL_NEED_COIN) {

            thinking_sdk_m.requestTrack('Shoping_Open', {
                OpenType: 'NoGold',
                OpenWay: this.openWay,
                IsNew: udm.isNew.value,
                Level_ID: udm.stageNum.value,
            });

            ShopPanel.StartProcess().subscribe();
            // BuyHeartPanel.CloseProcess(null, 'close');
        } else {
            let needCount = DevConfig.HEART_RESTORE_MAX_COUNT - udm.getHeartCount();
            if (needCount > 0) {

                thinking_sdk_m.requestTrack('Coins_Use', {
                    CoinNum: DevConfig.HEART_REFILL_NEED_COIN,
                    Type: 'Energy',
                    IsNew: udm.isNew.value,
                    Level_ID: udm.stageNum.value,
                });

                thinking_sdk_m.requestTrack('Get_Energy', {
                    Type: "Gold",
                    EnergyType: 'Num',
                    EnergyCount: needCount,
                    IsNew: udm.isNew.value,
                    Level_ID: udm.stageNum.value,
                });

                udm.incHeartCount(needCount);
                udm.coin.value -= DevConfig.HEART_REFILL_NEED_COIN;
                udm.save();
                BuyHeartPanel.CloseProcess(null, 'continue');
            }
        }
    }

    adsButtonClicked() {
        thinking_sdk_m.requestTrack('Ads_Click', {
            AdsType: 'AdEnergy',
            OpenUI: 'BuyHeartPanel',
            IsNew: udm.isNew.value
        });
        adsm.showRewardAds('stage_start_add_heart').subscribe(_ => {
            if (_ == 'success') {
                thinking_sdk_m.requestTrack('Ads_Return', {
                    AdsType: gm.currentAdsType.value,
                    OpenUI: gm.currentAdsOpenUI.value,
                    IsNew: udm.isNew.value
                });
                thinking_sdk_m.requestTrack('Get_Energy', {
                    Type: "Ads",
                    EnergyType: 'Num',
                    EnergyCount: 1,
                    IsNew: udm.isNew.value,
                    Level_ID: udm.stageNum.value,
                });
                udm.incHeartCount(1);
                BuyHeartPanel.CloseProcess(null, 'continue');
            }
        });
    }
}

